//
// Student 1 Name, ID.  SuJiani,20242996
// Student 2 Name, ID.
// Student 3 Name, ID.
//
#include <iostream>
#include <string>
#include <memory>
#include <signal.h>
#include "Book.h"
#include "Library.h"
#include <limits>

Library library;

enum class UserChoice {
    EXIT = 0,
    DISPLAY_BOOKS = 1,
    SEARCH_BOOK,
    SORT_BY_TITLE,
    SORT_BY_AUTHOR,
    SORT_BY_YEAR,
    SORT_BY_ISBN
   
};


int main()
{

    library.addBook("Book1", "Author1", 2005, "205079");
    library.addBook("Book2", "Author2", 1992, "199244");

    int choice;
    do {
        std::cout << std::endl << "Library Management System" << std::endl;
        std::cout << "1. Display Books" << std::endl; // This line can be modified
        std::cout << "2. Search for a Book" << std::endl; // This line can be modified
        std::cout << "3. Sort Books by Title" << std::endl; // This line can be modified
        std::cout << "4. Sort Books by Author" << std::endl; // This line can be modified
        std::cout << "5. Sort Books by Year" << std::endl; // This line can be modified
        std::cout << "6. Sort Books by ISBN" << std::endl; // This line can be modified
        std::cout << "0. Exit" << std::endl; // This line can be modified
        std::cout << "Enter your choice: ";
        std::cin >> choice;


        try
        {
            if (std::cin.fail()) 
            {
                throw std::invalid_argument("Invalid input. Please enter a valid number.");
            }

            switch (static_cast<UserChoice>(choice))
            {
            case UserChoice::DISPLAY_BOOKS:
                library.displayBooks();
                break;
            case UserChoice::SORT_BY_TITLE:
                library.sortByTitle();
                std::cout << "Books sorted by title.\n";
                break;
            case UserChoice::SORT_BY_AUTHOR:
                library.sortByAuthor();
                std::cout << "Books sorted by author.\n";
                break;
            case UserChoice::SORT_BY_YEAR:
                library.sortByYearOfPublication();
                std::cout << "Books sorted by year.\n";
                break;
            case UserChoice::SORT_BY_ISBN:
                library.sortByISBN();
                std::cout << "Books sorted by ISBN.\n";
                break;
            case UserChoice::SEARCH_BOOK: 
                {
                std::cout << "Enter title or ISBN to search: ";
                std::string query;
                std::cin.ignore();
                std::getline(std::cin, query);
                if (library.searchBook(query)) 
                {
                    std::cout << "Book found.\n";
                }
                else
                {
                    std::cout << "Book not found.\n";
                }
                break;
            }
            case UserChoice::EXIT:
               break;
            default:
                std::cout << "Invalid choice. Please try again.\n";
            }
        }
        catch (const std::invalid_argument& e) 
        {
            std::cerr << e.what() << std::endl;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }


    } while (choice != 0);

    return EXIT_SUCCESS;
}
